var express = require("express");
var router = express.Router();
var db = require("../sql.js");
/* GET users listing. */
router.get("/", function (req, res, next) {
  //   res.render('index');
  console.log("like:", req.query.like);
  var like = req.query.like || req.query.selectVal;
  var pageNum = req.query.page;

  db.query(
    `select * from banner where name like '%${like}%'`,
    function (err, result) {
      var pager = {};
      // 当前页码
      pager.pageCurrent = pageNum || 1;
      // 总条数
      pager.maxNum = result.length;
      // 每页条数
      pager.pageSize = 5;
      // 总页数
      pager.pageCount = parseInt(Math.ceil(pager.maxNum / pager.pageSize));
      // 页码列表
      var dataList = result.slice(
        (pager.pageCurrent - 1) * pager.pageSize,
        (pager.pageCurrent - 1) * pager.pageSize + pager.pageSize
      );

      if (err) {
        console.log(err);
        return;
      }
      res.render("bannerList", {
        bannerList: dataList,
        pager: pager,
      });
    }
  );
});
module.exports = router;
